package com.example.minefieldkeepercloud.activity.commonpool;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.activity.treatedmachine.TreateMachineAcDetail;
import com.example.minefieldkeepercloud.adapter.PoolDetailAdapter;
import com.example.minefieldkeepercloud.bean.PoolBean;
import com.example.minefieldkeepercloud.bean.PoolDetailBean;
import com.example.minefieldkeepercloud.interfac.ApiHttpCallback;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;
import com.example.minefieldkeepercloud.util.NetWorkUtil;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 常用矿池详情
 */
public class PoolDetailAc extends AppCompatActivity {
    @BindView(R.id.recyleview)
    LRecyclerView mRecyclerView;
    private TextView tv_pool,tv_createPerson,tv_customer,tv_state;
    private PoolDetailAdapter poolDetailAdapter;
    private List<PoolBean> listData;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private  PoolDetailBean poolDetailBean;
    public static void start(Activity mactivity, String Id) {
        Intent intent = new Intent(mactivity, PoolDetailAc.class);
        intent.putExtra("Id", Id);
        mactivity.startActivity(intent);

    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_pooldetail);
        ButterKnife.bind(this);
        initview();
        getNetData();
    }
    /**
     * 初始化
     */
    private void initview() {
//        EventBus.getDefault().register(this);
        listData=new ArrayList<>();
        poolDetailAdapter=new PoolDetailAdapter(this,listData);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(poolDetailAdapter);
        View header = LayoutInflater.from(this).inflate(R.layout.sample_header,(ViewGroup)findViewById(android.R.id.content), false);
        tv_pool=header.findViewById(R.id.tv_pool);
        tv_createPerson=header.findViewById(R.id.tv_createPerson);
        tv_customer=header.findViewById(R.id.tv_customer);
        tv_state=header.findViewById(R.id.tv_state);
        lRecyclerViewAdapter.addHeaderView(header);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(lRecyclerViewAdapter);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader); //设置下拉刷新Progress的样式
        // mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);  //设置下拉刷新箭头
        //设置头部加载颜色
        mRecyclerView.setHeaderViewColor(R.color.system_color, R.color.system_color, R.color.bg);
//设置底部加载颜色
        mRecyclerView.setFooterViewColor(R.color.system_color, R.color.system_color, android.R.color.white);
        mRecyclerView.setPullRefreshEnabled(false);
        mRecyclerView.setLoadMoreEnabled(false);


    }

    private void getNetData()
    {

        if (!NetWorkUtil.check(this)) {

            return;
        }
        NetHelperNew.GetWhiteOrder(getIntent().getStringExtra("Id"),new ApiHttpCallback(PoolDetailAc.this,1,true) {
            @Override
            public void onSuccess(String data) {
                poolDetailBean = MyGson.getInstance().fromJson(data, PoolDetailBean.class);
                if (poolDetailBean.getType() == 1) {
                    tv_pool.setText(poolDetailBean.getData().getKC());
                    tv_createPerson.setText(poolDetailBean.getData().getTGKH());
                    tv_customer.setText(poolDetailBean.getData().getMobilePhone());

                    if(poolDetailBean.getData().getSMSSwitch()==0)
                    {
                        tv_state.setText("关闭");
                        tv_state.setTextColor(getResources().getColor(R.color.red));
                    }
                    else
                    {
                        tv_state.setText("开启");
                        tv_state.setTextColor(getResources().getColor(R.color.system_color));
                    }

                    for(Map.Entry<String, String> entry :poolDetailBean.getData().getPool().entrySet()){
                        String str[];
                        str=entry.getKey().split("\\|");
                        PoolBean poolBean=new PoolBean();
                        poolBean.setKC(str[1]);
                        poolBean.setKGM(str[0]);
                        listData.add(poolBean);
                    }
                    poolDetailAdapter.notifyDataSetChanged();

                } else {
                    ToastUtil.showToast(PoolDetailAc.this, poolDetailBean.getContent());
                }
                mRecyclerView.refreshComplete(10);
            }


        });



    }

    @OnClick({R.id.title_left,R.id.ll_date})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left:
                finish();
                break;
            case R.id.ll_date:
                Intent intent=new Intent(PoolDetailAc.this, SetPool.class);
                Bundle bd=new Bundle();
                bd.putSerializable("data",poolDetailBean);
                intent.putExtra("bd",bd);
                startActivity(intent);
                overridePendingTransition(0, 0);
                break;
        }
    }

//    @Subscribe(threadMode = ThreadMode.MAIN, sticky = false, priority = 0)
//    public void handleData(String str) {
//
//        if (str.equals("10115")) {
//            listData.clear();
//            getNetData();
//        }
//
//
//    }
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        EventBus.getDefault().unregister(this);
//    }
}
